<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class StaffOrg
 * @package App\Models
 * @version December 5, 2018, 11:33 am CST
 *
 * @property string name
 * @property float fee_annual
 * @property float fee_annual_oversize
 * @property float fee_monthly
 * @property float fee_monthly_oversize
 * @property integer parent_id
 */
class StaffOrg extends Model
{
    use SoftDeletes;

    public $table = 'staff_org';
    
    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';


    protected $dates = ['deleted_at'];


    public $fillable = [
        'name',
        'fee_annual',
        'fee_annual_oversize',
        'fee_monthly',
        'fee_monthly_oversize',
        'parent_id'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'name' => 'string',
        'fee_annual' => 'float',
        'fee_annual_oversize' => 'float',
        'fee_monthly' => 'float',
        'fee_monthly_oversize' => 'float',
        'parent_id' => 'integer'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [

    ];

    // 获取组织结构树
    public static function getNodes($oid = 0, &$orgs = []){
        // $oid = ($oid == 0) ? $this->oid : $oid;

        $org = self::select('id', 'name', 'parent_id')->where('id', $oid)->first();
        if ($org) {
            array_unshift($orgs, $org->toArray());
            if ($org->parent_id != 0) {
                return self::getNodes($org->parent_id, $orgs);
            }  
        } 
        return $orgs;
    }

    
}
